package cn.th.service.impl;

import cn.th.entity.Dormitory;
import cn.th.entity.Myclass;
import cn.th.mapper.MyclassMapper;
import cn.th.service.IMyclassService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Service
public class MyclassServiceImpl extends ServiceImpl<MyclassMapper, Myclass> implements IMyclassService {

    @Autowired
    private IMyclassService myClassService;

    @Autowired
    private MyclassMapper myclassMapper;
    @Override
    public ResponseEntity<String> saveFile(MultipartFile file) {
        try {
            // 读取Excel文件
            InputStream inputStream = file.getInputStream();
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历Excel表格数据,从第二行开始读
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                Row row = sheet.getRow(i);

                String school = row.getCell(0).getStringCellValue(); // 获取每行的第一个单元格中的内容

                String name = row.getCell(1).getStringCellValue(); // 获取每行的第二个单元格中的内容

                String academy = row.getCell(2).getStringCellValue(); // 获取每行的第三个单元格中的内容

                String professional = row.getCell(3).getStringCellValue(); // 获取每行的第四个单元格中的内容

                Integer total = (int) row.getCell(4).getNumericCellValue(); // 获取每行的第五个单元格中的内容

                // 进行操作，对new出来的Myclass进行赋值
                Myclass myclass = new Myclass();
                myclass.setSchool(school);
                myclass.setName(name);
                myclass.setAcademy(academy);
                myclass.setProfessional(professional);
                myclass.setTotal(total);

                //myclass.setTotal(Integer.valueOf(total));

                // 将数据存入数据库
                myClassService.save(myclass);
            }
            return ResponseEntity.ok("Excel上传成功");
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel上传失败");
        }
    }

    /**
     * 通过id批量删除班级
     * @param ids
     */
    @Override
    public void removeBatchByIds(String ids) {
        myclassMapper.deleteBatchByIds(ids);
    }

    @Override
    public Map<String, Object> queryMyClass(int page, int pageSize, String school, String name, String academy, String professional, String total) {
        //构造分页构造器
        Page<Myclass> page1 = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Myclass> wrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        wrapper.like(StringUtils.hasLength(school), Myclass::getSchool, school); //学校支持模糊查询
        wrapper.eq(StringUtils.hasLength(name), Myclass::getName, name);
        wrapper.eq(StringUtils.hasLength(academy),Myclass::getAcademy,academy);
        wrapper.eq(StringUtils.hasLength(professional),Myclass::getProfessional,professional);
        wrapper.eq(StringUtils.hasLength(total),Myclass::getTotal,total);

        //添加排序条件,根据id升序排
        wrapper.orderByAsc(Myclass::getId);

        //执行查询
        myClassService.page(page1,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total",page1.getTotal());
        data.put("rows",page1.getRecords());

        return data;
    }
}
